<template>
	<div class="category-list">
		<list-title :data="header"></list-title>
		<el-input placeholder="输入关键字进行过滤" v-model="filterText" style="margin-top: 10px"></el-input>
		<div class="tree-title">
			<span>栏目名称</span>
			<span class="content">
				<span style="width: 50px">ID</span>
				<span style="width: 50px">外链</span>
				<span style="width: 60px">类型</span>
				<span style="width: 80px">内容总数</span>
				<span style="width: 80px">预览总数</span>
				<span>显示状态</span>
				<span style="width: 150px">操作</span>
			</span>
		</div>
		<el-tree
		:data="categoryList"
		node-key="catid"
		:default-expand-all="true"
		:expand-on-click-node="false"
		:filter-node-method="filterNode"
		v-loading="loading"
		element-loading-text="拼命加载中"
	    element-loading-spinner="el-icon-loading"
	    element-loading-background="rgba(255, 255, 255, 0.92)"
	    @node-drop="handleDrop"
	    draggable
		:allow-drop="allowDrop"
		ref="tree">
			<span class="custom-tree-node" slot-scope="{ node, data }">
				<span>{{ node.label.name }}</span>
				<span class="content">
					<span style="width: 50px">
						{{ data.catid }}
					</span>
					<span style="width: 50px">
						<span :class="'el-icon-' + ((data.outside == 1)?'close':'check')"></span>
					</span>
					<span style="width: 60px">
						{{ (data.flag == '1')?'单页':'列表' }}
					</span>
					<span style="width: 80px">
						{{ data.content_count }}
					</span>
					<span style="width: 80px">
						{{ data.preview_count }}
					</span>
					<span>
						<el-switch v-model="data.display" @change="changeDisplay(node.key, data)"></el-switch>
					</span>
					<span style="width: 150px">
						<el-button type='text' size="mini" @click="handleEdit(node, data)" style="margin-left: 0px" v-if="auth.editor">编辑</el-button>
						<el-popover
						placement="top"
						width="160"
						v-model="delTip[node.key]" v-if="auth.delete">
							<p class="del-tip"><span class="el-icon-question"></span>确定删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="delTip.splice(node.key, 1, false)">取消</el-button>
								<el-button type="primary" size="mini" @click="handleDelete(node.key, data)">确定</el-button>
							</div>
							<el-button type="text" slot="reference" :disabled='submitLock[node.key]'>
								删除 <span class="el-icon-loading" v-if='submitLock[node.key]'></span>
							</el-button>
						</el-popover>
					</span>
				</span>
			</span>
		</el-tree>
	</div>
</template>

<script>
import ListTitle from "pc/common/Title"
import ListTable from "pc/common/ListTable"
import {changeToBool, judgeAuth} from "static/js/common.js"
import {mapGetters} from 'vuex'

export default {
	inject: ['reload'],
	data() {
		return {
			/*头部数据*/
			header: {
				title: '栏目列表',
				reload: true,
				buttons: [
					{
						title: '添加栏目',
						type: 'primary',
						pathName: 'pc/epi/pj8/GoodsCategory',
						auth: true,
					}
				]
			},
			auth: {
				editor: true,//编辑
				delete: true,//删除
				changeState: true,//显示状态
				sort: true,//排序
				union: true,//自定义字段关联
			},
			dialog: {
				'title': '设置自定义字段',
				'visible': false,
				'parentid': 0,//用于 同步上级栏目自定义字段
				'catid': 0,
			},
			/*自定义字段列表数据*/
			categoryAttrList: [
				// {
				// 	id: 1,
				// 	attr_name: '',
				// 	attr_type: '',
				// 	state: '',
				// },
			],
			/*弹窗中 选择好的自定义字段*/
			categoryAttr: '',
			loading: true,
			filterText: '',//搜索关键词
			categoryList: [],
			tableLoading: true,
			delTip: [],//刪除提示
			submitLock: [],
			/*自定义字段列表*/
			categoryAttrRelationships: [
				// {
				// 	'id': 0,
				// 	'attr_id': 0,
				// 	'attr_name': '',
				// }
			],
			categoryAttrRelationshipsSubmitLock: false,
		};
	},
	components: {
		ListTitle,
		ListTable
	},
	created: function() {
		/*获取所有栏目数据*/
		this.getCategoryList()
		/*权限判断显示*/
		// this.header.buttons[0].auth = (judgeAuth('addContentCategory', this.allMenuList) == undefined)?false:true
		// this.auth.editor = (judgeAuth('modifyContentCategory', this.allMenuList) == undefined)?false:true
		// this.auth.delete = (judgeAuth('delGoodsCategory', this.allMenuList) == undefined)?false:true
		// this.auth.changeState = (judgeAuth('setGoodsCategoryDisplay', this.allMenuList) == undefined)?false:true
		// this.auth.sort = (judgeAuth('goodsCategorySort', this.allMenuList) == undefined)?false:true
		// this.auth.union = (judgeAuth('setContentCategoryAttrRelationships', this.allMenuList) == undefined)?false:true
	},
	watch: {
		/*搜索关键词*/
		filterText(val) {
			this.$refs.tree.filter(val);
		},
	},
	methods: {
		/*获取栏目*/
		getCategoryList: function(parentid = 0){
			if(this.$api.goodsCategoryList != undefined){
				this.$get(this.$api.goodsCategoryList, {
					// 'parentid': parentid,
					'type': 'ele',
					'display': 0,
				}).then(res => {
					// console.log(res);
					if(res.data.code == 10007){//后台提示需要登录
						this.$cookie.remove('isLogin');//刪除cookie登录信息
						this.reload();
					}
					if(res.data.code === 0){
						if(res.data.data != undefined){
							this.categoryList = res.data.data
						}
						this.tableLoading = false
					}
				})
				.catch(err => {})
				.finally(() => {
					this.loading = false
				}) 
			}
		},
		/*编辑栏目*/
		handleEdit(index, row) {
			// console.log(row.catid);
			this.$router.push(
				{
					name: 'pc/epi/pj8/GoodsCategory', 
					query: {
						catid: row.catid,
					}
				}
			)
		},
		/*删除栏目*/
		handleDelete(index, row) {
			// console.log(index)
			if(this.$api.delGoodsCategory != undefined){
				if(!this.submitLock[index]){
					this.submitLock[index] = true//锁住当前的 删除按钮
					this.delTip.splice(index, 1, false)//提示消失
					/*请求后台删除*/
					this.$get(this.$api.delGoodsCategory, {
						'catid': row.catid
					}).then(res => {
						if(res.data.code == 10007){//后台提示需要登录
							this.$cookie.remove('isLogin');//刪除cookie登录信息
							this.reload();
						}
						if(res.data.code == 0){
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'success'
							});
							this.reload();
						}else{
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'error'
							});
						}
						this.submitLock = []
						this.submitLock[index] = false//释放当前的 删除按钮
					})
					.catch(err => {})
					.finally(() => {}) 
				}
			}
		},
		/*过滤节点*/
		filterNode(value, data) {
			if (!value) return true;
			return data.label.name.indexOf(value) !== -1;
		},
		/*列表中直接修改 是否显示*/
		changeDisplay(index, row){
			if(this.$api.setGoodsCategoryDisplay != undefined){
				/*请求后台*/
				this.$post(this.$api.setGoodsCategoryDisplay, {
					'catid': row.catid,
					'display': row.display
				}).then(res => {
					if(res.data.code == 10007){//后台提示需要登录
						this.$cookie.remove('isLogin');//刪除cookie登录信息
						this.reload();
					}
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}else{
				this.$message({
					showClose: true,
					message: '您没有设置状态的权限',
					type: 'error'
				});
			}
		},
		//拖拽完毕 调用分类排序接口
		handleDrop(draggingNode, dropNode, dropType, ev) {
			if(this.$api.goodsCategorySort != undefined){
				// console.log(dropNode.data)
				this.$post(this.$api.goodsCategorySort, {
					'sort': draggingNode.data.catid,
					'target': dropNode.data.catid,
					'location': dropType
				}).then(res => {
					if(res.data.code == 10007){//后台提示需要登录
						this.$cookie.remove('isLogin');//刪除cookie登录信息
						this.reload();
					}
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		//判断拖动是否满足条件
		allowDrop(draggingNode, dropNode, type) {
			if(type !== 'inner' && draggingNode.data.parentid == dropNode.data.parentid){//不能进入内部、必须是同一目录
				return true;
			}
		}
	},
	computed: {
		...mapGetters([
			'allMenuList'
		])
	}
};
</script>

<style lang="stylus" scoped>
.category-list
	.tree-title
		flex: 1
		display: flex
		align-items: center
		justify-content: space-between
		padding: 10px 0
		border-bottom: 1px solid #e9ecf4
		font-size: 12px
		font-weight: bold
		color: #909399
		.content
			span
				width: 100px
				display: inline-block
				margin-right: 5px
			span:last-child
				margin-right: 0
				width: 200px
	.el-tree
		/deep/ .el-tree-node__content
			padding: 18px 0
			border-bottom: 1px solid #e9ecf4
		.custom-tree-node
			flex: 1
			display: flex
			align-items: center
			justify-content: space-between
			font-size: 12px
			.content
				>span
					width: 100px
					display: inline-block
					margin-right: 5px
					// border: 1px solid red
				>span:last-child
					margin-right: 0
					.el-button--small
						padding: 0
				>span.icon-sort-up,span.icon-sort-down
					width: 16px
					margin-right: 0px
					color: $theme-color
</style>